package com.example.controller;

import cn.hutool.core.map.MapUtil;
import com.example.POJO.User;
import com.example.Util.JwtUtils;
import com.example.common.Result;
import com.example.common.dto.LoginDto;
import com.example.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(value="/register"  )
public class Register {

    @Autowired
    UserRepository userRepository;

    @Autowired
    JwtUtils jwtUtils;


    @RequestMapping(value="", method= RequestMethod.POST    )
    public Result login(@Validated @RequestBody LoginDto loginDto, HttpServletResponse response) {
        User user =  userRepository.findUserByUsername(loginDto.getUsername());
        if (user != null){
            return Result.fail("用户已经存在");
        }

        int ii = userRepository.add_user(loginDto.getUsername(), loginDto.getPassword());

        if (ii == 1){
            return Result.succ("注册成功!!");
        }
        return Result.fail("未知错误");
    }




}
